package pl.edu.mimuw.irs.core.model.design.resource;

import java.util.Arrays;
import java.util.Collection;

/**
 * @author Jakub Rauch
 * Created on: 2009-05-07
 */
public class Requirement extends AbstractRolesHolder implements IMember {

	private static final long serialVersionUID = 8430054870597839096L;
	
	// *** Attributes
	
	private Requirements requirements;
	// Getters and setters
	public Requirements getRequirements() {
		return requirements;
	}
	@SuppressWarnings("unchecked")
	public IHolder getHolder() {
		return getRequirements();
	}
	public void setRequirements(Requirements requirements) {
		if (this.requirements == requirements) return;
		Requirements oldParent = null;
		if (this.requirements != null) oldParent = this.requirements;
		this.requirements = requirements;
		if (oldParent != null) oldParent.remove(this);
	}
	
	// *** Constructors

	public Requirement(Collection<RoleInstance> roleInstances) {
		super(roleInstances);
	}
	
	public Requirement(RoleInstance... roleInstances) {
		this(Arrays.asList(roleInstances));
	}
	
	public Requirement(String name) {
		this((Collection<RoleInstance>)null);
		setName(name);
	}
	
	public Requirement() {
		this((String)null);
	}
	
	// *** Methods
	
	@Override
	protected String getNamePrefix() {
		return "Req";
	}
	
	public IMember copy() {
		Requirement requirement = new Requirement();
		for (RoleInstance roleInstance : this.getRolesMap().values()) {
			requirement.add(roleInstance.copy());
		}
		return requirement;
	}

	public void delete() {
		getRequirements().remove(this);
	}
	/*
	@Override
	public int hashCode() {
		int hashCode = 0;
		for (RoleInstance instance : getRolesMap().values()) {
			hashCode += instance.hashCode();
		}
		return hashCode;
	}
	
	@Override
	public boolean equals(Object arg0) {
		if (arg0 instanceof Requirement) {
			Requirement requirement = (Requirement) arg0;
			if (requirement.getRolesMap().size() != getRolesMap().size()) 
				return false;
			
			Map<Role, RoleInstance> rolesMap2 = requirement.getRolesMap();
			for (RoleInstance instance : getRolesMap().values()) {
				if (!rolesMap2.containsValue(instance)) return false;
			}
			
			return true;
		}
		return false;
	}
	*/
}
